package com.baselibrary.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;

import com.baselibrary.R;
import com.baselibrary.utils.ScreenUtils;
import com.baselibrary.view.DoubleClickListener;
import com.baselibrary.view.MaskImageView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片详情浏览
 */
public class LocusImagePageAdapterNew extends PagerAdapter {


    private List<String> mImgList = new ArrayList<>();
    private final Activity context;
    private final int viewPagerHeight;
    private OnDClickListener onDClickListener;

    public OnDClickListener getOnDoubleClickListener() {
        return onDClickListener;
    }

    public void setOnDClickListener(OnDClickListener onDoubleClickListener) {
        this.onDClickListener = onDoubleClickListener;
    }

    public LocusImagePageAdapterNew(List<String> mImgList, int pagerMaxHeight, Activity context) {
        this.mImgList = mImgList;
        this.context = context;
        this.viewPagerHeight = pagerMaxHeight;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_locus_img, container, false);
        ImageView img = view.findViewById(R.id.iv_picture);
        MaskImageView mIvGround = view.findViewById(R.id.iv_ground);
        int imgHeight = Integer.parseInt(ScreenUtils.getVideoHight(mImgList.get(position % mImgList.size())));
        int imgWidth = Integer.parseInt(ScreenUtils.getVideoWidth(mImgList.get(position % mImgList.size())));
        ScreenUtils.setImageHeight(context, img, imgWidth, imgHeight, viewPagerHeight);
        Glide.with(context).load(mImgList.get(position % mImgList.size()))
                .placeholder(R.drawable.ic_def_hanxiang_228)
                .apply(mIvGround.setGaussBlur())
                .into(mIvGround);
        Glide.with(context).load(mImgList.get(position % mImgList.size())).into(img);

        mIvGround.setOnClickListener(new DoubleClickListener(new DoubleClickListener.DoubleClickCallBack() {
            @Override
            public void oneClick(View view) {
                if (onDClickListener != null)
                    onDClickListener.onSingleClick();
            }

            @Override
            public void doubleClick(View view) {
                if (onDClickListener != null)
                    onDClickListener.onDoubleClick();
            }
        }));

        container.addView(view, 0);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
//        return mImgList.size() > 1 ? Integer.MAX_VALUE : mImgList.size();
        return mImgList.size();
    }


    public interface OnDClickListener {
        void onDoubleClick();

        void onSingleClick();
    }
}